0% completed
Problem Statement
You are given two integers arrays arr1 and arr2. All elements of the arr2 are distinct, and present in arr1.
Sort the array arr1 such that relative orders of elements of arr1 should be the same as in arr2. If any elements in arr1 not found in arr2 should come at the end, sorted in ascending order.
Examples
Example 1:
- Input: arr1 =
[8, 1, 2, 3, 4, 8, 9], arr2 =[4, 3, 2, 1] - Expected Output:
[4, 3, 2, 1, 8, 8, 9] - Justification: The elements in
arr1that are also inarr2are arranged in thearr2order first (4, 3, 2, 1), followed by the remaining elements ofarr1in ascending order (8, 8, 9).
Example 2:
- Input: arr1 =
[5, 5, 4, 6, 4], arr2 =[4, 5] - Expected Output:
[4, 4, 5, 5, 6] - Justification: First, we arrange the common elements in the order they appear in
arr2(4, 4, 5, 5), then append the remaining elements ofarr1sorted (6).
Example 3:
- Input: arr1 =
[10, 20, 30, 20, 15, 20, 10], arr2 =[20, 10] - Expected Output:
[20, 20, 20, 10, 10, 15, 30] - Justification: We follow the order in
arr2for common elements (20, 20, 20, 10, 10), then sort the rest (15, 30).
Solution
To solve this problem, we'll employ a custom sorting strategy that prioritizes the sequence specified in arr2 for the elements found in both arrays. This approach works because it allows us to maintain the relative order of arr2 in arr1 while ensuring that elements not present in arr2 are sorted in ascending order at the end of arr1. This method is efficient and straightforward because it directly addresses the problem's requirements by treating it as a two-part task: first, reordering according to arr2, and second, sorting the remaining elements.
In the initial phase, we'll map the elements of arr2 with their corresponding indices to efficiently check the order of elements that appear in arr1. For elements not in arr2, a separate list will be maintained and sorted later. This segregation ensures that we can easily append the sorted list of non-arr2 elements to the reordered list of arr1 elements. This strategy is effective because it minimizes the need for complex sorting logic, relying instead on the inherent order of arr2 and standard sorting for the remaining elements.
Step-by-step Algorithm
-
Initialize a HashMap (
orderMap) to map each element inarr2to its index. This will help us quickly identify the order ofarr2elements inarr1. -
Create two Lists (
arr2OrderListandnonArr2List):arr2OrderListto hold elements found in botharr1andarr2, to maintain the order specified byarr2.nonArr2Listto hold elements that are inarr1but not inarr2, which will be sorted in ascending order later.
-
Iterate through
arr1:- For each element, check if it exists in
orderMap(meaning it's also inarr2).- If yes, add it to
arr2OrderList. - If no, add it to
nonArr2List.
- If yes, add it to
- For each element, check if it exists in
-
Sort
nonArr2Listin ascending order since these elements do not have a specified order fromarr2. -
Sort
arr2OrderListbased on the indices stored inorderMapto maintain the order specified byarr2. -
Merge the two lists: First, add all elements from
arr2OrderListto the final result array, then append all elements fromnonArr2List. -
Return the merged array as the final sorted array.
Algorithm Walkthrough
Let's consider the Input:
arr1 = [10, 20, 30, 20, 15, 20, 10]arr2 = [20, 10]
Step-by-Step Walkthrough:
-
Initialize
orderMap:orderMap = {20: 0, 10: 1}
-
Create Lists:
arr2OrderList = []nonArr2List = []
-
Iterate through
arr1:- For each element in
arr1, check if it exists inorderMap:10is inarr2, add toarr2OrderList→arr2OrderList = [10]20is inarr2, add toarr2OrderList→arr2OrderList = [10, 20]30is not inarr2, add tononArr2List→nonArr2List = [30]- Another
20, add toarr2OrderList→arr2OrderList = [10, 20, 20] 15is not inarr2, add tononArr2List→nonArr2List = [30, 15]- Another
20, add toarr2OrderList→arr2OrderList = [10, 20, 20, 20] - Another
10, add toarr2OrderList→arr2OrderList = [10, 20, 20, 20, 10]
- After completing the iteration:
arr2OrderListcontains[10, 20, 20, 20, 10]nonArr2Listcontains[30, 15]
- For each element in
-
Sort
nonArr2Listin ascending order:- After sorting,
nonArr2Listbecomes[15, 30].
- After sorting,
-
Sort
arr2OrderListbased on the order inarr2:- To sort
arr2OrderListbased onarr2's order, we arrange the elements according to their index inorderMap. Since20has a lower index (0) than10(1),20should come before10. - After sorting based on
arr2's order,arr2OrderListshould be rearranged to[20, 20, 20, 10, 10].
- To sort
-
Merge
arr2OrderListandnonArr2Listfor the final result:- Merge the sorted
arr2OrderListwith the sortednonArr2List. - The final result array after merging:
[20, 20, 20, 10, 10, 15, 30].
- Merge the sorted
-
Return the final sorted array.
Code
Complexity Analysis
- Time Complexity: The time complexity of this algorithm is
, where N is the number of elements in arr1. This is because we sort the elements ofarr1that are not found inarr2. The mapping and iteration steps are linear in complexity. - Space Complexity: The space complexity is
, as we store the elements of arr1in separate lists or arrays before combining them into the final result array.
On this page
Problem Statement
Examples
Solution
Step-by-step Algorithm
Algorithm Walkthrough
Code
Complexity Analysis